package com.shelpe.services.sinbad.controller.actionLog.impl.campaign;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.controller.actionLog.ActionLogDto;
import com.shelpe.services.sinbad.controller.actionLog.ActionLogService;
import com.shelpe.services.sinbad.controller.actionLog.CampaignVo;
import com.shelpe.services.sinbad.entity.jpa.AreaList;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.CampaignLog;

@Service
public class CampaignAddConverter extends ActionLogService {
	@Autowired
	private MessageSource messageSource;

	@Override
	public ActionLogDto convertCampaign(CampaignLog entity,Map<Long,Campaign> campaignMap,List<AreaList> areas) {
		boolean campaignAddConverter = entity.getAction() == CampaignLog.Action.ADD;
		if (campaignAddConverter) {
			if (StringUtils.isNotBlank(entity.getNote())) {
				ActionLogDto dto = new ActionLogDto();
				dto.from(entity);
				CampaignVo vo = this.unserializeCampaign(entity.getNote());
				if (vo != null && StringUtils.isNotBlank(vo.getTitle())) {
					String[] params = { vo.getTitle() };
					dto.setReason(messageSource.getMessage("message.campaign.add.reason", params, Locale.ENGLISH));
					return dto;
				}
			}
		}
		return null;
	}
}
